<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	if(!$_RQ['id']){
		exi('请选择动态！','error');
	}
	$this->session('pid',$_RQ['id']);
	$item=pdo_get('jy_ailiao_newslog',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$item['content']=str_replace("\n","</br>",$item['content']);
	$item['video']=tomedia($item['video'],true);
	$item['images']=json_decode($item['images'],true);
	$item['orderfee']=pdo_getsum('jy_merch_game_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'fee');
	$item['sendtypetitle']=$this->sendtypes[$item['sendtype']]?:'上门自提';
	$return['item']=$item;
	if($return['item']['mid']==$_SESSION['uid']){
		$return['item']['ismy']=1;
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$_RQ['id']);
	$return['params']=$params;
	$return['likesnum']=pdo_count('jy_ailiao_likenews',$params,'id');
	$return['msgnum']=pdo_count('jy_ailiao_newslog_msg',$params,'id');
	$link=custom_url('jy_ailiao_newslog_msg/item//id='.$_RQ['id'].'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true);
	$return['sharedata']=array(
			'title'=>$item['title'],
			'desc'=>$return['item']['desc']?:'商家节假日 老带新 裂变引流 成交复购必备工具',
			'link'=>$link,
			'imgUrl'=>tomedia($item['logo']),
	);
	set_html('title',$item['title']);
	exi($return);
}
if($_RQ['op']=='getmsg'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$_RQ['pid']);
	if($_RQ['gamename']){
		$params['gamename']=$_RQ['gamename'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_ailiao_newslog_msg',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$lasttime=TIMESTAMP-$item['createtime'];
			$item['lasttime']=$lasttime;
			if($lasttime>24*3600){
				$item['days']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['days']=intval($lasttime/3600).'小时前';
			}else{
				$item['days']=intval($lasttime/60).'分钟前';
			}
			$item['createtime']=date('Y-m-d',$item['createtime']);
			$item['member'] = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
			$item['member']['avatar']=tomedia($item['member']['avatar']);
			$item['member']['nickname']=$item['member']['nickname']?cfc('tools')->substr($item['member']['nickname'],0,1).'***':'无';
		}
	}	
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='like'){
	$pid=$_RQ['id'];
	$newslog=pdo_get('jy_ailiao_newslog',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']));
	$isadd=pdo_get('jy_ailiao_likenews',array('mid'=>$_SESSION['uid'],'pid'=>$pid,'uniacid'=>$_SESSION['uniacid']));
	if($isadd['id']){
		pdo_delete('jy_ailiao_likenews',array('mid'=>$_SESSION['uid'],'pid'=>$pid,'uniacid'=>$_SESSION['uniacid']));
		exi('取消成功！');
	}else{
		$data=array(
			'mid'=>$_SESSION['uid'],
			'fmid'=>$newslog['mid'],
			'pid'=>$pid,
			'uniacid'=>$_SESSION['uniacid'],
			'createtime'=>time(),
		);
		pdo_insert('jy_ailiao_likenews',$data);
	}
	exi('点赞成功！');
}
if($_RQ['op']=='guanz'){
	$tomid=$_RQ['mid'];
	$isadd=pdo_get('jy_ailiao_likes',array('mid'=>$_SESSION['uid'],'tomid'=>$tomid,'uniacid'=>$_SESSION['uniacid']));
	if($isadd['id']){
		pdo_delete('jy_ailiao_likes',array('mid'=>$_SESSION['uid'],'tomid'=>$tomid,'uniacid'=>$_SESSION['uniacid']));
		exi('取消关注成功！');
	}else{
		$data=array(
			'mid'=>$_SESSION['uid'],
			'tomid'=>$tomid,
			'uniacid'=>$_SESSION['uniacid'],
			'createtime'=>time(),
		);
		pdo_insert('jy_ailiao_likes',$data);
	}
	exi('关注成功！');
}
if($_RQ['op']=='setmsg'){
	$pid = $this->session('pid');
	$postdata=get_postdata();
	$status = ($basic['msg_shenhe'] || $basic['open_wxappsh'])?0:1;
	$data=array(
		'title'=>trim($postdata['msg']),
		'images'=>ijson_encode($postdata['images']),
		'mid'=>$_SESSION['uid'],
		'pid'=>$pid,
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>$status,
		'createtime'=>time(),
	);
	pdo_insert('jy_ailiao_newslog_msg',$data);
	if($status){
		exi('评论成功！');
	}else{
		exi('评论成功！请等待审核');
	}
}
if($_RQ['op']=='setend'){
	$pid = $this->session('pid');
	$update=array(
		'onlinetime'=>time()-100,
	);
	pdo_update('jy_ailiao_newslog',$update,array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']));
	exi('结束活动成功！');
}